//Tranz-Am
//version 2. Loads up the map and lets cursor keys navigate the map
#include "game_transam.h"

int main(void)
{
	//initialise graphics and animations, set up buffers and ready for action
	//we will allow any changes to the config file, cos we're hard
	//also there is no bitmap loader as all we want is the bitmaps loaded from a bitmap file
	//in this case, it's a sheet graphic - only one graphic file for the whole project, plus the panel
	GameFramework=new Framework("config.xml","animations.xml");

	//some kind of error
	if(Configuration::GlobalErrorString!="")
	{
		delete GameFramework;
		allegro_message(Configuration::GlobalErrorString.c_str());
	}
	else
	{
		//initialise some stuff first
		//this will always grab the pointer to the animation library and the configuration library
		//so we can do stuff with them, like display animations, update the configuration, read custom variables
		if(!SetupGame())
		{
			//failed for some reason
			delete GameFramework;
			allegro_message(Configuration::GlobalErrorString.c_str());
		}
		else
		{
			//this function invokes the FPS based timer loop. 
			//we do not require ESCape to be defaulted as an escape from the whole system
			//as we are coding that ourselves
			//we only want one logic/drawing function hook and we've assigned it to the 'GAME' enumeration 
			//doesn't matter what we do as the names ar arbitrary
			//The two functions mentioned are in game_game.cpp
			if( !GameFramework->StartGameLoop(
					NULL,NULL,
					NULL,NULL,
					TransAmGameLogic,TransAmGameDrawing,
					NULL,NULL,
					false) ) 
			{
				delete GameFramework;
				allegro_message("Failed to run game loop. Check config.log");
			}
			else 
			{
				//when the game has finished this is called
				//endgame will destroy all the stuff we created
				EndGame();
				//this will destroy all animations and configurations, and shut down allegro
				delete GameFramework;
				
				//any final errors
				if(Configuration::GlobalErrorString!="")
				{
					Configuration::LogEntry(Configuration::GlobalErrorString);
					delete GameFramework;
					allegro_message(Configuration::GlobalErrorString.c_str());
				}
				
			}
		}
	}
	allegro_exit();
	return 0;
}
END_OF_MAIN();

/**********************************************************************************
 our helper functions, the same as the animation functions, but using the framework
*/
bool SetupGame()
{
	srand ( time(NULL) );

	//after the framework is initialised we need to access the animations and configuration objects
	//the objects returned are the same as those if we created configuration and animation objects ourselves as in example 2
	GameLibrary=GameFramework->GetGraphicsLibrary();
	GameConfiguration=GameFramework->GetConfiguration();

	//animations
	//initialise single use graphics in game
	DisplayPanel		=new Animation(GameLibrary,"panel");
	
	if(Configuration::GlobalErrorString!="") 
	{
		Configuration::LogEntry("animation initialisation failed in setup",false,"transam.log");
		EndGame();
		return false;
	}

	//debug to see the graphics and log to a file
	//GameLibrary->draw();
	//GameLibrary->DebugData();

	//map positions	
	//draw map to the right of the display panel
	ScreenXOffset=DisplayPanel->ReadOnly_CurrentItem->w;
	ScreenYOffset=0;
	ShownMapWidth=GameConfiguration->CapsGraphics.ScrWidth-ScreenXOffset;
	ShownMapHeight=GameConfiguration->CapsGraphics.ScrHeight-ScreenYOffset;
	
	Configuration::LogEntry("initialised",false,"transam.log");
	
	return true;
}

void EndGame()
{
	//only delete the stuff we create
	//the framework deletes the drawing buffers, animation, framework, configuration
	MapFreeMem();	//free up mappy memory
	if(DisplayPanel) delete DisplayPanel;
	Configuration::LogEntry("terminated",false,"transam.log");
}

